package com.school.PersonalAddressBook;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try (Connection conn = DatabaseConnection.getConnection()) {
            while (true) {
                System.out.println("1. Add Contact\n2. Update Contact\n3. Delete Contact\n4. List Contacts\n5. Exit");
                int choice = Integer.parseInt(scanner.nextLine());
                switch (choice) {
                    case 1:
                        addContact(conn, scanner);
                        break;
                    case 2:
                        updateContact(conn, scanner);
                        break;
                    case 3:
                        deleteContact(conn, scanner);
                        break;
                    case 4:
                        listContacts(conn);
                        break;
                    case 5:
                        return;
                    default:
                        System.out.println("Invalid choice!");
                }
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    private static void addContact(Connection conn, Scanner scanner) throws SQLException {
        System.out.print("Enter Name: ");
        String name = scanner.nextLine();
        System.out.print("Enter Address: ");
        String address = scanner.nextLine();
        System.out.print("Enter Phone: ");
        String phone = scanner.nextLine();
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO contacts(name, address, phone) VALUES (?, ?, ?)");
        stmt.setString(1, name);
        stmt.setString(2, address);
        stmt.setString(3, phone);
        stmt.executeUpdate();
    }

    private static void updateContact(Connection conn, Scanner scanner) throws SQLException {
        System.out.print("Enter ID to update: ");
        int id = Integer.parseInt(scanner.nextLine());
        System.out.print("Enter New Name: ");
        String name = scanner.nextLine();
        System.out.print("Enter New Address: ");
        String address = scanner.nextLine();
        System.out.print("Enter New Phone: ");
        String phone = scanner.nextLine();
        PreparedStatement stmt = conn.prepareStatement("UPDATE contacts SET name=?, address=?, phone=? WHERE id=?");
        stmt.setString(1, name);
        stmt.setString(2, address);
        stmt.setString(3, phone);
        stmt.setInt(4, id);
        stmt.executeUpdate();
    }

    private static void deleteContact(Connection conn, Scanner scanner) throws SQLException {
        System.out.print("Enter ID to delete: ");
        int id = Integer.parseInt(scanner.nextLine());
        PreparedStatement stmt = conn.prepareStatement("DELETE FROM contacts WHERE id=?");
        stmt.setInt(1, id);
        stmt.executeUpdate();
    }

    private static void listContacts(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM contacts");
        while (rs.next()) {
            Contact contact = new Contact(rs.getInt("id"), rs.getString("name"), rs.getString("address"), rs.getString("phone"));
            System.out.println(contact);
        }
    }
}